package com.hzhjhh.view.cashierView;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.JTableHeader;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.hzhjhh.init.dialog.Mytable;
import com.hzhjhh.init.dialog.SettleDialog;
import com.hzhjhh.init.dialog.Vendition_dialog_jTextField5_keyAdapter;
import com.hzhjhh.means.Insert_stock;
import com.hzhjhh.means.UpdateData;
import com.hzhjhh.view.cashierView.constant.ShowConstants;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

/**
 * 前台收银页面
 * 
 * @author Administrator
 *
 */
@Getter
@Setter
public class VenditionDialog extends JFrame {
	
	/**
	 * 登录页面有没有初始化的标识
	 * 	false-没有初始化（默认值），true-已经初始化成功了
	 */
	private boolean adminInitFlag = false;
	/**
	 * 商品信息展示每一行记录结果
	 */
	private Vector<Vector<String>> rowDatas = new Vector<>();

	JPanel panel1 = new JPanel();
	JScrollPane jScrollPane1 = new JScrollPane();
	JTextField txfMerchandiseCode ;
	JLabel lbMerchandiseCode ;
	JTextField txfMerchandiseNum ;
	JTextField txfPricePerOne;
	JLabel lbMerchandiseNum ;
	JLabel lbPricePerOne = new JLabel();
	JLabel lbDiscount = new JLabel();
	JLabel lbReceivable = new JLabel();
	JLabel lbReceivable2 = new JLabel();
	JLabel lbGiveChange = new JLabel();
	JLabel lbGiveChaneg2 = new JLabel();
	JLabel lbActualReceivable = new JLabel();
	TitledBorder titledBorder1 = new TitledBorder("");

	JTable jTable1 = new JTable();
	JTableHeader jTableHeader1 = jTable1.getTableHeader();

	Vector headV = new Vector(); // 表头
	Vector colnames1 = new Vector(); // 测试
	Vector colnames2 = new Vector(); // 测试
	Vector colnames4 = new Vector();
	JTextField txfActualReceivable = new JTextField();
	JButton lbDelMerchand = new JButton();
	JButton lbCancelDeal = new JButton();
	JButton front2Back = new JButton();
	JTextField txfVipCardId = new JTextField();
	JLabel lbVipCardId = new JLabel();
	JLabel lbCurrentTime2 = new JLabel();
	JLabel lbCurrentTime = new JLabel();
	JTextField txfDiscount = new JTextField();

	private VenditionDialog(Frame owner, String title, boolean modal) {
//		super(owner, title, modal);
		try {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jbInit();
			pack();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	public VenditionDialog() {
		this("超市前台收银系统");
	}
	public VenditionDialog(String title) {
		this(new Frame(), "前台收银程序", true);
		this.setSize(800, 600);
		this.setLocation(50, 50);
		this.setVisible(true);
		this.setTitle(title);

	}

	private void jbInit() throws Exception {
		panel1.setLayout(null);
		txfPricePerOne = PricePerOneInit.initTxf();
		txfActualReceivable = ActualReceivableInit.initTxf(this);

		txfActualReceivable.addKeyListener(new Vendition_dialog_jTextField5_keyAdapter(this));
		lbDelMerchand = DelMerchandiseInit.initBt(this);

		lbCancelDeal = CancelDealInit.initBt(txfVipCardId, jTable1, jScrollPane1);
		front2Back = Front2BackInit.initBt(this);
		lbDiscount = MerchandiseDiscountInit.initLb();
		txfDiscount = MerchandiseDiscountInit.initTxf();
		txfVipCardId = VipCardIdInit.initTxf(txfDiscount);
		lbVipCardId = VipCardIdInit.initLb();
		lbReceivable = ReceivableInit.initLb();
		lbReceivable2 = ReceivableInit.initLb2();
		txfMerchandiseCode = MerchandiseCodeInit.initTxf(txfPricePerOne);
		txfMerchandiseNum = MerchandiseNumInit.initTxf(this);
		lbCurrentTime2 = CurrentTimeInit.initLb2();
		lbCurrentTime = CurrentTimeInit.initLb();
		this.getContentPane().add(panel1, java.awt.BorderLayout.CENTER);
		lbMerchandiseCode = MerchandiseCodeInit.initLabel();
		lbMerchandiseNum = MerchandiseNumInit.initLabel();
		lbPricePerOne = PricePerOneInit.initLabel();
		lbGiveChange = GiveChangeInit.initLb();
		lbGiveChaneg2 = GiveChangeInit.initLb2();
		lbActualReceivable = ActualReceivableInit.initLb();
		Timer timer = new Timer();
		timer.schedule(new RemindTask(), 0, 1000); // 得到当前时间
		panel1.add(jScrollPane1);
		jScrollPane1.getViewport().add(jTable1);
		panel1.add(lbReceivable);
		panel1.add(lbReceivable2);
		panel1.add(lbActualReceivable);
		panel1.add(txfActualReceivable);
		panel1.add(lbGiveChange);
		panel1.add(lbGiveChaneg2);
		panel1.add(txfMerchandiseNum);
		panel1.add(lbMerchandiseCode);
		panel1.add(txfMerchandiseCode);
		panel1.add(lbMerchandiseNum);
		panel1.add(txfVipCardId);
		panel1.add(lbVipCardId);
		panel1.add(txfPricePerOne);
		panel1.add(lbPricePerOne);
		panel1.add(lbDiscount);
		panel1.add(lbDelMerchand);
		panel1.add(front2Back);
		panel1.add(lbCancelDeal);
		panel1.add(lbCurrentTime2);
		panel1.add(lbCurrentTime);
		panel1.add(txfDiscount);
		this.getContentPane().add(jTableHeader1, java.awt.BorderLayout.NORTH);
		headV = ShowConstants.MERCHANDISE_HEAD;

		colnames2.add(colnames1);
		jTable1 = Mytable.maketable(new Vector<>(), headV); // 显示内容
		jScrollPane1.getViewport().add(jTable1); // 把表装入容器
		jScrollPane1.setBounds(new Rectangle(24, 28, 746, 419));
		panel1.setBackground(Color.pink);
	}

	class RemindTask extends TimerTask {
		public void run() {
			lbCurrentTime2.setText(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		}
	}

	public void jTextField5_keyReleased(KeyEvent e) {
		int t = (int) e.getKeyChar();
		if (t == KeyEvent.VK_ENTER) { // 如果输入的是回车键
			try {
				Double gather = new Double(txfActualReceivable.getText());
				double gathering = ((int) (gather * 100)) / 100.0;
				Double account = new Double(lbReceivable2.getText());
				double givechange = ((int) ((gathering - account) * 100)) / 100.0;
				if (givechange < 0) {
					JOptionPane.showMessageDialog(this, "输入的金额不足", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				Double givechange1 = new Double(givechange);

				lbGiveChaneg2.setText(givechange1.toString());
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(this, "输入的金额有误", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			SettleDialog sett = new SettleDialog(lbReceivable2.getText(), txfActualReceivable.getText(), lbGiveChaneg2.getText());
			txfVipCardId.setEditable(true); // 会员卡号可编辑
			txfVipCardId.setText("");
			// 商品编号 销售日期 商品数量 商品单价 折扣 会员卡号 商品名称
			int num1 = 0;
			while (num1 < rowDatas.size()) { // 更新数据库
				Vector ls = new Vector();
				ls = (Vector) rowDatas.get(num1);
				// ls.get(0).toString();//商品编号
				// jLabel11.getText();//时间
				Integer number = new Integer(ls.get(4).toString()); // 数量
				Double Price = new Double(ls.get(5).toString()); // 单价
				// ls.get(6).toString();//折扣
				// jTextField6.getText();//会员卡号
				// ls.get(1).toString();//商品名称
				UpdateData.out_StockPile(number, ls.get(0).toString());
				Insert_stock.Insert_Sale(ls.get(0).toString(), lbCurrentTime2.getText(), number, Price, ls.get(6).toString(),
						txfVipCardId.getText(), ls.get(1).toString());
				num1++;
			}
			txfMerchandiseCode.setText("");
			txfMerchandiseNum.setText("1");
			rowDatas.removeAllElements();
			jTable1 = Mytable.maketable(rowDatas, headV); // 显示内容
			jScrollPane1.getViewport().add(jTable1); // 把表装入容器

		}

	}

}
